package com.diandiansong.app.ui.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.diandiansong.app.R;
import com.diandiansong.app.entity.MyInfo;
import com.diandiansong.app.ui.cart.CartMain;
import com.diandiansong.app.ui.index.MsgList;
import com.diandiansong.app.ui.index.Order;
import com.diandiansong.app.utils.A;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import diandiansong.library.ui.base.BaseFrag;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;

import static diandiansong.library.ui.base.BaseAct.setText;

/**
 * 我的
 */
public class UserMain extends BaseFrag {

    @BindView(R.id.iv_avatar)
    ImageView mIvAvatar;
    @BindView(R.id.tv_shop_name)
    TextView mTvShopName;
    @BindView(R.id.tv_invite_code)
    TextView mTvInviteCode;
    Unbinder unbinder;

    MyInfo mMyInfo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.user_main, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refresh();
    }

    private void refresh() {
        A.my(new CCallBack<MyInfo>() {
            @Override
            public void onSuccess(MyInfo o) throws Exception {
                mMyInfo = o;
                updateUI();
            }

            @Override
            public void onFailure(int code, String msg) {
                toast(msg);
            }
        });
    }

    @OnClick(R.id.go_system_setting)
    public void onViewClicked() {
        SystemSetting.start(getActivity());
    }

    @OnClick({R.id.go_order, R.id.go_cart, R.id.go_userinfo, R.id.go_msg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.go_order:
                Order.start(getActivity());
                break;
            case R.id.go_cart:
                CartMain.start(getActivity());
                break;
            case R.id.go_userinfo:
                if (mMyInfo != null) {
                    UserInfo.start(getActivity(), mMyInfo);
                } else {
                    A.my(new CCallBack<MyInfo>() {
                        @Override
                        public void onSuccess(MyInfo o) throws Exception {
                            mMyInfo = o;
                            updateUI();
                            UserInfo.start(getActivity(), mMyInfo);
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            toast(msg);
                            toast("获取资料失败");
                        }
                    });

                }
                break;
            case R.id.go_msg:
                MsgList.start(getActivity());
                break;
        }
    }

    private void updateUI() {
        MyInfo.Data data = mMyInfo.getData();
        ImgLoader.displayRound(mIvAvatar, data.getAvatar(), R.drawable.img47);
        mTvShopName.setText(data.getCompany());

        String invitation_code = data.getInviterCode();
        if (invitation_code != null && !invitation_code.isEmpty()) {
            mTvInviteCode.setVisibility(View.VISIBLE);
            setText(mTvInviteCode, "邀请码：" + invitation_code);

        } else {
            mTvInviteCode.setVisibility(View.GONE);
        }
    }

    public static class Refresh {
    }

    public static void doRefresh() {
        EventBus.getDefault().post(new Refresh());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiceRefresh(Refresh refresh) {
        refresh();
    }
}
